package com.xbmzdx.fstj_app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbmzdx.fstj_app.service.WeatherService;
import com.xbmzdx.fstj_app.service.photocheck.CheckPhotoService;
import com.xbmzdx.fstj_app.service.photocheck.uploadFileToOss;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/weather")
public class WeatherController {

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private CheckPhotoService checkPhotoService;

    @PostMapping("/findWeather")
    @ApiOperation("根据城市获取天气")
    public String findWeather(String address,@DefaultValue("null")String city) throws Exception {
        Map map;
        if (city==null|| city.equals ("")){
            map= weatherService.testGetParameter (address);
            String weather = weatherService.findWeather (map.get ("lat").toString (),map.get ("lng").toString ());
            return weather;
        }else {
            map = weatherService.testGetParameter (address, city);
            String weather =weatherService.findWeather (map.get ("lat").toString (),map.get ("lng").toString ());
            return weather;
        }
    }

//    @PostMapping("/checkPhoto")
//    @ApiOperation("检查图片是否违规")
//    public JSONObject checkPhoto(@RequestPart(value = "file") MultipartFile file) {
//        String path = checkPhotoService.fileUpload (file);
//        return JSON.parseObject(checkPhotoService.imgCensor(path));
//    }
//
//
//    @ApiOperation(value = "上传图片")
//    @PostMapping(value = "/fileUpload")
//    public String uploadPhoto(@RequestPart(value = "file") MultipartFile file) throws IOException {
//        String path = checkPhotoService.fileUpload (file);
//        FileInputStream fileInputStream = new FileInputStream(path);
//        return "https://test11268.oss-cn-beijing.aliyuncs.com/"+uploadFileToOss.uploadFileToOss ("oss-cn-beijing.aliyuncs.com","LTAI5tFjWmQHDfZEuGXKebbL","9EwvdfVNsQ8b1vFgLtE6bGLos0kxaU",fileInputStream,
//                "test11268","",file.getOriginalFilename ());
//    }
}
